package webdev.danagtravel;

import webdev.common.GPS;
import webdev.dto.DiaDiemDTO;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.tyczj.mapnavigator.Navigator;

import dev.danang.R;

public class MapDetail extends FragmentActivity implements OnClickListener {

	Bundle b;
	
	private ImageView mBtnBack;
	private TextView mTxtTitle;
	
	// Google Map
	private GoogleMap gMap;
	private LatLng POINT_PLACE;
	private LatLng POINT_CURRENT;
	
	private DiaDiemDTO diaDiem;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_detail);
		init();
		setValue();
		setEvent();
	}
	
	private void init() {
		mBtnBack = (ImageView) findViewById(R.id.img_back);
		mTxtTitle = (TextView) findViewById(R.id.text_view_header_title);
		b = this.getIntent().getExtras();
		diaDiem = b.getParcelable("diadiem");
		
		if(gMap == null) {
			gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_place_detail)).getMap();
		}
		
	}
	
	private void setValue() {
		try {
			POINT_PLACE = new LatLng(diaDiem.getLat(), diaDiem.getLng());
			
			gMap.addMarker(new MarkerOptions()
			.position(POINT_PLACE)
			.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
			.title(diaDiem.getTitle()));
			
			CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(POINT_PLACE, 14);
			gMap.moveCamera(cameraUpdate);
			
			GPS gps = new GPS(getApplicationContext());
			if(gps.canGetLocation()) {
				double lat = gps.getLatitude();
				double lng = gps.getLongitude();
				
				POINT_CURRENT = new LatLng(lat, lng);
				gMap.addMarker(new MarkerOptions()
				.position(POINT_CURRENT)
				.title("You are here")
				.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
				
				Navigator nav = new Navigator(gMap, POINT_PLACE, POINT_CURRENT);
				nav.findDirections(true);
			} else {
				gps.showSettingsAlert();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		mTxtTitle.setText(diaDiem.getTitle());
	}
	
	private void setEvent() {
		mBtnBack.setOnClickListener(this);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			onBackPressed();
			break;

		default:
			break;
		}
	}
}
